package com.ruoyi.travel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.travel.domain.bo.WechatUserBo;
import com.ruoyi.travel.service.IWechatUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.travel.domain.vo.WechatUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 租户控制器
 * 前端访问路由地址为:/system/user
 *
 * @author ruoyi
 * @date 2024-02-07
 */
@Slf4j
@Tag(name = "租户")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class WechatUserController extends BaseController {

    private final IWechatUserService iWechatUserService;

//    @DubboReference
    private final RemoteDictService remoteDictService;

    /**
     * 查询租户列表
     */
    @SaIgnore
    @Operation(summary = "查询租户列表")
    @SaCheckPermission("system:user:list")
    @GetMapping("/list")
    public TableDataInfo<WechatUserVo> list(WechatUserBo bo, PageQuery pageQuery) {
        List<SysDictData> sys_user_sex = remoteDictService.selectDictDataByType("sys_user_sex");
        log.debug("sys_user_sex={}",sys_user_sex);
        return iWechatUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出租户列表
     */
    @SaCheckPermission("system:user:export")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WechatUserBo bo, HttpServletResponse response) {
        List<WechatUserVo> list = iWechatUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "租户", WechatUserVo.class, response);
    }

    /**
     * 获取租户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:user:query")
    @GetMapping("/{id}")
    public R<WechatUserVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iWechatUserService.queryById(id));
    }

    /**
     * 新增租户
     */
    @SaCheckPermission("system:user:add")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WechatUserBo bo) {
        return toAjax(iWechatUserService.insertByBo(bo));
    }

    /**
     * 修改租户
     */
    @SaCheckPermission("system:user:edit")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WechatUserBo bo) {
        return toAjax(iWechatUserService.updateByBo(bo));
    }

    /**
     * 删除租户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:user:remove")
    @Log(title = "租户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWechatUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
